using UnityEngine;
using System.Collections;

[RequireComponent(typeof(CharacterController))]
public class PlayerControl : StateBehaviour<PlayerControl>
{
    #region Attributes

    // Control
    public CharacterController characterController;
    public Vector3 targetSpeed = Vector3.zero;
    public float groundGravityPercent = 0.0f;
    public float airGravityPercent = 1.0f;

    // Stats
    public float health = 100.0f;
    public int lives = 3;
    public float stamina = 100.0f;
    public float staminaMax = 100.0f;
    public float staminaRegen = 10.0f;

    // Costs
    public float runStaminaCost = 1.0f;
    public float jumpStaminaCost = 1.0f;

    // Idle
    public float idleAnimationTime1 = 8.0f;
    public float idleAnimationTime2 = 8.0f;

    // Walk
    public float walkSpeed = 3.0f;

    // Run
    public float runSpeed = 6.0f;

    // Jump
    public float jumpForce = 10.0f;
    public float airMovePercentage = 0.7f; //Porcentaje de movimiento horizontal en el aire (0.0 - 1.0)
    public float jumpGravityPercentage = 2.0f; //Ajusta la velocidad general del salto (0.0 - 1.0)

    // Hang
    public Vector3 hangPointOffset = new Vector3(0.0f, 1.0f, 0.0f);
    public GameObject hands = null;

    // Attack
    public GameObject weapon = null; // Para probar
    //public MeleeWeapon meleeWeaponData = null;

    #endregion

    #region Props

    public bool NoStamina
    {
        get
        {
            return this.stamina <= 0;
        }
    }

    #endregion

    #region Methods

    public void Move(bool isJumping, float currentSpeed, float gravityPercentage)
    {        
        this.targetSpeed.x = Input.GetAxis(Constants.HORIZONTAL_AXIS) * currentSpeed;
        if (isJumping)
            this.targetSpeed.x *= this.airMovePercentage;

        this.targetSpeed.y -= Constants.GRAVITY * Time.deltaTime * gravityPercentage;

        // Para mantenerme pegado al suelo
        if (this.State == PlayerWalkState.Instance || this.State == PlayerRunState.Instance || this.State == PlayerIdleState.Instance)
            this.targetSpeed.y = -1.0f;

        this.characterController.Move(this.targetSpeed * Time.deltaTime);
    }

    public void Attack()
    {
        this.weapon.transform.localRotation = Quaternion.Euler(0, 0, 90); // Para pruebas        
        this.weapon.GetComponent<MeleeWeapon>().AttackStart();
    }

    public void StaminaLoose(float staminaCost)
    {
        this.stamina -= staminaCost;
    }

    public void TakeDamage(float damage)
    {
        //this.State = PlayerTakeDamage.Instance;
    }

    #endregion

    #region UnityMethods

    void Start()
    {
        characterController = GetComponent<CharacterController>();
        // Estado inicial
        this.State = PlayerIdleState.Instance;
    }

    void LateUpdate()
    {
        if (this.stamina < this.staminaMax)
            this.stamina += this.staminaRegen * Time.deltaTime;
        else
            this.stamina = this.staminaMax;
    }

    void OnDrawGizmos()
    {
        //Debug.DrawRay(this.hands.transform.position, this.transform.forward, Color.red);
    }

    void OnTriggerEnter(Collider other)
    {
        if (other.name.Contains(Constants.BORDER_NAME))
        {
            this.State.OnTriggerState(this, other);
        }
    }

    #endregion

    #region StateBehaviour

    public override PlayerControl GetBehaviuor()
    {
        return this;
    }

    #endregion
}
